/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "maya_local.h"

/*-----------------------------------------------------------------------------
	Maya scene export (transformation stuff):
-----------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
	Transformation and animation stuff :
-----------------------------------------------------------------------------*/

//
//	GetTransformMatricies	
//	see: MFnTransform for details :	
//
static void GetTransformMatricies	( MObject &obj, 
	MMatrix	&Sp,	MMatrix &St,	MMatrix &S,		MMatrix	&Sh,
	MMatrix &Rp,	MMatrix	&Rt,	MMatrix	&Ro,	MMatrix &R,		MMatrix	&T	)
{																					
	MFnTransform	transform(obj);
	
	Sp	=	MMatrix::identity;
	St	=	MMatrix::identity;
	S	=	MMatrix::identity;
	Sh	=	MMatrix::identity;
	Rp	=	MMatrix::identity;
	Rt	=	MMatrix::identity;
	Ro	=	MMatrix::identity;
	R	=	MMatrix::identity;
	T	=	MMatrix::identity;
	
	MPoint		sp;		sp	=	transform.scalePivot( MSpace::kTransform );
	MVector		s;				transform.getScale( &s.x );
	MVector		sh;				transform.getShear( &sh.x );
	MPoint		spt;	spt	=	transform.scalePivotTranslation( MSpace::kTransform );
	MPoint		rp;		rp	=	transform.rotatePivot( MSpace::kTransform );
	MQuaternion	ro;		ro	=	transform.rotateOrientation( MSpace::kTransform );
	MQuaternion	r;				transform.getRotation( r, MSpace::kTransform );
	MPoint		rpt;	rpt	=	transform.rotatePivotTranslation( MSpace::kTransform );
	MVector		t;		t	=	transform.getTranslation( MSpace::kTransform );
	MTransformationMatrix::RotationOrder	rot_order;
	rot_order	=	 transform.rotationOrder();


	Sp(3,0)	=	sp.x / sp.w;		St(3,0)	=	spt.x / spt.w;
	Sp(3,1)	=	sp.y / sp.w;		St(3,1)	=	spt.y / spt.w;
	Sp(3,2)	=	sp.z / sp.w;		St(3,2)	=	spt.z / spt.w;
	
	S(0,0)	=	s.x;				Sh(1,0)	=	sh.x;
	S(1,1)	=	s.y;				Sh(2,0)	=	sh.y;
	S(2,2)	=	s.z;				Sh(2,1)	=	sh.z;
	
	Rp(3,0)	=	rp.x / rp.w;		Rt(3,0)	=	rpt.x / rpt.w;
	Rp(3,1)	=	rp.y / rp.w;		Rt(3,1)	=	rpt.y / rpt.w;
	Rp(3,2)	=	rp.z / rp.w;		Rt(3,2)	=	rpt.z / rpt.w;
	
	Ro	=	ro.asMatrix();
	R	=	r.asMatrix();
	
	T(3,0)	=	t.x;
	T(3,1)	=	t.y;
	T(3,2)	=	t.z;
}	


//
//	EMLevelExporter::ParseTransform
//
MStatus EMSceneExporter::ParseTransform( IPxSceneNode scene_node, MObject &obj, EMatrix &scale_matrix )
{
	MFnTransform	transform(obj);

	MMatrix		Sp;					MMatrix		St;
	MMatrix		S;					MMatrix		Sh;
	MMatrix		Rp;					MMatrix		Rt;
	MMatrix		Ro;
	MMatrix		R;
	MMatrix		T;
	
	GetTransformMatricies( obj, Sp, St, S, Sh, Rp, Rt, Ro, R, T );	

	MMatrix	finS	=	Sp.inverse() * S * Sh * Sp * St;
	MMatrix	finR	=	Rp.inverse() * Ro * R * Rp * Rt;
	MMatrix	finT	=	T;

	//	not used :	
	MMatrix	TM1	=	 finS * finR * finT; 

	//	store scale matrix to scale mesh later	
	scale_matrix			=	MMatrix2EMatrix4( finS );

	//	NOTE : geometry must be scaled before exporting, 
	//	so keep rotation and translation only :
	EMatrix		TM			=	MMatrix2EMatrix4( finR * finT );

	EPoint		pos		=	EPoint( TM(3,0), TM(3,1), TM(3,2), TM(3,3) );
	EQuaternion	orient	=	EQuaternion::FromMatrix( MMatrix2EMatrix4( finR ) );	

	//	store computed values :
	scene_node->SetLocalPose(pos, orient);

	return MS::kSuccess;
}

